package com.ac.api.log.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.helper.DateUtils;
import com.base.helper.StringUtils;
import com.ac.api.log.dao.AcLogMapper;
import com.ac.api.log.entity.AcLog;
import com.ac.api.log.service.AcLogService;
import com.ac.api.log.vo.AcLogVo;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mzl
 * @since 2020-07-09
 */
@Service
public class AcLogServiceImpl extends ServiceImpl<AcLogMapper, AcLog> implements AcLogService {

    public List<String> getDays(String startTime, String endTime) {
        // 返回的日期集合
        List<String> days = new ArrayList<String>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return days;
    }

    @Override
    public List<AcLogVo> findByExample(AcLogVo example) {
        return baseMapper.findByExample(buildWrapper(example));
    }

    @Override
    public Page<AcLogVo> findByPage(AcLogVo example, Page page) {
        page.setOptimizeCountSql(false);
        return page.setRecords(baseMapper.findByExample(buildWrapper(example), page));
    }

    @Override
    public List<Integer> checkUpdatePhoto(Map<Integer, String> photos) {
        for (Map.Entry<Integer, String> idPhoto : photos.entrySet()) {
            Integer examineeid = idPhoto.getKey();
            AcLogVo example    = new AcLogVo();
            Integer urlStart   = idPhoto.getValue().indexOf("upload\\\\202104");
            String  subUrl     = idPhoto.getValue().substring(urlStart + 18);
            String  photoName  = subUrl.substring(0, 32);
            example.setParams(photoName);
            List<AcLogVo> acLogs = this.baseMapper.findByExample(buildWrapper(example));
            if (null != acLogs && acLogs.size() > 0) {
                for (AcLogVo acLog : acLogs) {
                    String param = acLog.getParams();
                    if ("enExaminee:update".equals(acLog.getAction())) {
                        Integer start = param.indexOf("\"id\":");
                        String  sub   = param.substring(start + 5);
                        String  id    = sub.substring(0, sub.indexOf(",\""));
                        if (examineeid != Integer.parseInt(id)) {
                            System.out.println("id和图片不匹配");
                        }
                    } else if ("enSubjectselect:joinExam".equals(acLog.getAction())) {
                        Integer start = param.indexOf("\"examineeid\":");
                        String  sub   = param.substring(start + 13);
                        String  id    = sub.substring(0, sub.indexOf(",\""));
                        if (examineeid != Integer.parseInt(id)) {
                            System.out.println("id和图片不匹配");
                        }
                    } else {
                        System.out.println(acLog.getAction());
                    }
                }
            }
        }
        return null;
    }

    private QueryWrapper<AcLogVo> buildWrapper(AcLogVo example) {
        QueryWrapper<AcLogVo> wrapper       = new QueryWrapper<>(example);
        Integer               acoperator    = example.getAcoperator();
        String                username      = example.getUsername();
        String                realname      = example.getRealname();
        String                grouptype     = example.getGrouptype();
        String                grouptypeName = example.getGrouptypeName();
        String                usergroup     = example.getUsergroup();
        String                usergroupName = example.getUsergroupName();
        String                params        = example.getParams();
        Date                  createtime    = example.getCreatetime();
        Date                  endtime       = example.getEndtime();
        wrapper.like(StringUtils.isNotEmpty(grouptypeName), "grouptype_name", grouptypeName)
                .like(StringUtils.isNotEmpty(usergroupName), "usergroup_name", usergroupName);
        wrapper.lambda()
                .eq(null != acoperator, AcLogVo::getAcoperator, acoperator)
                .like(StringUtils.isNotEmpty(username), AcLogVo::getUsername, username)
                .like(StringUtils.isNotEmpty(realname), AcLogVo::getRealname, realname)
                .like(StringUtils.isNotEmpty(params), AcLogVo::getParams, params)
                .eq(StringUtils.isNotEmpty(grouptype), AcLogVo::getGrouptype, grouptype)
                .eq(StringUtils.isNotEmpty(usergroup), AcLogVo::getUsergroup, usergroup)
                .orderByAsc(AcLogVo::getId);
        if (null != createtime) {
            wrapper.lambda()
                    .le(AcLogVo::getCreatetime, DateUtils.toLastSecondOfTheDay(endtime))
                    .ge(AcLogVo::getCreatetime, DateUtils.toFirstSecondOfTheDay(createtime));
        }
        wrapper.lambda().orderByDesc(AcLogVo::getCreatetime);
        return wrapper;
    }
}
